package com.pachong.service.impl;

import com.common.core.utils.Checkbox;
import com.domain.bean.sys.SysRole;
import com.domain.bean.sys.SysUser;
import com.pachong.common.impl.BaseServiceImpl;
import com.pachong.service.SysUserService;
import com.wy.dao.sys.BaseDAO;
import com.wy.dao.sys.SysUserDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * Author : wangyu
 * Date: 2018-01-26
 * Time: 15:49
 */
@Service
public class SysUserServiceImpl extends BaseServiceImpl<SysUser,String> implements SysUserService {


    @Autowired
    private SysUserDAO sysUserDAO;

    @Override
    public BaseDAO<SysUser, String> getBaseDao() {
        return sysUserDAO;
    }

    @Override
    public SysUser login(String username) {

        return sysUserDAO.findByUserName(username);
    }

    @Override
    public boolean checkUser(String userName) {
        SysUser su = new SysUser();
        su.setUserName(userName);
        Example<SysUser> example = Example.of(su);
        return sysUserDAO.exists(example);
    }

    @Override
    public List<Checkbox> getUserRoleByJson(String userId) {
        SysUser user = sysUserDAO.findOne(userId);
        Set<SysRole> roleList=user.getRoles();
        List<Checkbox> checkboxList=new ArrayList<>();
        for(SysRole sysRole:roleList){
            Checkbox checkbox=new Checkbox();
            checkbox.setId(sysRole.getId());
            checkbox.setName(sysRole.getRoleName());
            checkbox.setCheck(true);
            checkboxList.add(checkbox);
        }
        return checkboxList;
    }

    @Override
    public void rePass(String userId,String newPassWord) {
        sysUserDAO.rePass(userId,newPassWord);
    }
}
